package com.niiwoo.civet.user.service.duboo;

import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.user.dao.entity.IdentityInfo;
import com.niiwoo.civet.user.dao.entity.OrganizationInfo;
import com.niiwoo.civet.user.dao.entity.UserBasicInfo;
import com.niiwoo.civet.user.dao.mapper.UserBasicInfoMapperExt;
import com.niiwoo.civet.user.dto.response.UserAccountPropertyResponse;
import com.niiwoo.civet.user.service.UserAccountDubboService;
import com.niiwoo.civet.user.service.local.OrganizationService;
import com.niiwoo.civet.user.service.local.UserIdentityService;
import com.niiwoo.tripod.lanmao.enums.UserTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

@Slf4j
@Service(version = "1.0.0", validation = "true")
public class UserAccountDubboServiceImpl implements UserAccountDubboService {

    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private UserBasicInfoMapperExt userBasicInfoMapperExt;
    @Autowired
    private UserIdentityService userIdentityService;

    @Override
    public UserAccountPropertyResponse getAccountProperty(String userId) {
        UserAccountPropertyResponse result = new UserAccountPropertyResponse();

        //先查询机构表(因为有部分机构信息同时在机构和个人表中都存在)
        OrganizationInfo organizationInfo = organizationService.getByOrgId(userId);
        if(Objects.nonNull(organizationInfo)){
            result.setUserType(UserTypeEnum.ORGANIZATION);
            result.setUserId(userId);
            result.setMobileNo(organizationInfo.getAgentPhoneNumber());
            result.setHeadUrl(organizationInfo.getHeadImage());
            result.setRealName(organizationInfo.getAbbreviation());//机构简称
            result.setNickName(maskCompanyName(organizationInfo.getOrganizationName()));
            return result;
        }else {
            //机构信息不存在，则查询个人信息
            UserBasicInfo userBasicInfo = userBasicInfoMapperExt.selectByPrimaryKey(userId);
            if(Objects.nonNull(userBasicInfo) ){
                result.setUserType(UserTypeEnum.PERSONAL);
                result.setUserId(userId);
                result.setMobileNo(userBasicInfo.getMobileNo());
                result.setHeadUrl(userBasicInfo.getHeadImage());

                IdentityInfo identityInfo = userIdentityService.getByUserId(userId);
                if(Objects.nonNull(identityInfo)){
                    result.setRealName(identityInfo.getName());
                }
                result.setNickName(userBasicInfo.getNickName());
                return result;
            }
        }

        log.warn("没有获取到用户对应的类型记录 getAccountProperty userId ={}",userId);
        return null;
    }

    /**
     * 公司名称打码
     *
     * @param companyName
     * @return
     */
    private static String maskCompanyName(String companyName) {
        if (StringUtils.isBlank(companyName) || companyName.length() <= 4) {
            return companyName;
        }
        return companyName.substring(0, 2) + "***" + companyName.substring(companyName.length() - 2, companyName.length());
    }
}
